package com.csu.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DeviceUtil {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 格式化接收时间
     * @param tdate
     * @return
     */
    public static String formatCreateDate(String tdate) throws Exception{
        if (StringUtil.isEmpty(tdate)) return null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = formatter.parse(tdate);
        return simpleDateFormat.format(date);
    }

    /**
     * @Author: lds
     * @Description: 判断是否在晚上12点到早上5点之间
     * @Date: Created in 10:33 2019/11/6
     * @param []
     * @return boolean
     */
    public static boolean timeBettow(){
        //获取晚上12点
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(cal.YEAR), cal.get(cal.MONTH), cal.get(cal.DATE), 0, 0, 0);
        long startTime = cal.getTimeInMillis();

        //当前时间
        Calendar ca = Calendar.getInstance();
        long nowTime = ca.getTimeInMillis();

        //获取早上5点
        Calendar ca2 = Calendar.getInstance();
        ca2.set(ca2.get(ca2.YEAR), ca2.get(ca2.MONTH), ca2.get(ca2.DATE), 5, 0, 0);
        long endTime = ca2.getTimeInMillis();

        if(nowTime>startTime&&nowTime<endTime){
            return true;
        }

        return false;
    }


    public static Map<String,Object> getLevel(long time){
        Map<String,Object> result=new HashMap<>();
        Calendar cal = Calendar.getInstance();
        long timeInMillis = cal.getTimeInMillis();
        long l = timeInMillis - time;
        if(l>3600000){
            result.put("level",3);
            result.put("remark","离床1小时");
        }else if(l>1800000){
            result.put("level",2);
            result.put("remark","离床半小时");
        }else if(l>600000){
            result.put("level",1);
            result.put("remark","离床10分钟");
        }else {
            result.put("level",0);
            result.put("remark","");

        }
        return result;
    }


    /**
     * 格式化接收时间
     * @param tdate
     * @return
     */
    public static Date formatCreateByDate(String tdate) throws Exception{
        if (StringUtil.isEmpty(tdate)) return null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return formatter.parse(tdate);
    }
}
